package client.backend.account;

import client.backend.Connection;
import server.conn.user.UserConnectionThread;
import java.io.IOException;
import java.util.LinkedList;

/**
 * Account stores the type of currency and the amount of it the user has at a specific time.
 *
 */
public class Account {
	private String symbol;
	private float amount;
	private long timestamp;
	
	/**
	 * Generates new currency account for a single currency. Gets data from server.
	 * @param symbol	Currency symbol.
	 */
	public Account(String symbol) {
		try {
			String split[] = (Connection.getInstance().readFromServer(UserConnectionThread.ACCOUNT_GET_ONE+UserConnectionThread.SPLIT+symbol)).split(UserConnectionThread.SPLIT);
		this.symbol = split[0];
		this.amount = new Float(split[1]);
		this.timestamp = new Integer(split[2]);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Creates a new currency account with data from parameters.
	 * @param symbol	Currency symbol.
	 * @param amount	Amount of currency.
	 * @param timestamp	Timestamp.
	 */
	public Account(String symbol, float amount, long timestamp) {
		this.symbol = symbol;
		this.amount = amount;
		this.timestamp = timestamp;
	}
	
	/**
	 * Generates a LinkedList of Account(s) for each transaction of this type of currency..
	 * @param limit	Length of history.
	 * @return		Returns LinkedList of Accounts.
	 */
	public LinkedList<Account> getHistory(int limit){
		String[] accounts = Connection.getInstance().readArray(UserConnectionThread.ACCOUNT_GET_HISTORY+UserConnectionThread.SPLIT+symbol+UserConnectionThread.SPLIT+limit);
		
		LinkedList<Account> history = new LinkedList<Account>();
		
		for(String element : accounts){
			String[] current = element.split(UserConnectionThread.SPLIT);
			history.add(new Account(current[0],new Integer(current[1]),new Long(current[2])));
		}
		return history;
		
	}
	
	/**
	 * Getter method for symbol.
	 * @return	Symbol of currency in account.
	 */
	public String getSymbol() { return symbol; }

	/**
	 * Getter method for symbol.
	 * @return	Amount of currency.
	 */
	public float getAmount(){ return amount; }
	
	/**
	 * Getter method for timeStamp.
	 * @return	Time of transaction.
	 */
	public long getTimeStamp() { return timestamp; }
}
